﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web;
using curbside.Web.UI.Script;

namespace curbside.Web.UI.WebControls
{
    internal class helper
    {
        public static void AddLinkToHeader(string key, string src)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                AddLinkToHeader(key, src, page);
            }
        }

        public static void AddLinkToHeader(string key, string src, Page page)
        {
            CheckParams(key, src);
            src = wrapSrc(src, page);

            if (page.Header != null)
            {
                if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), key))
                {
                    page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, String.Empty);
                    ((HtmlHead)page.Header).Controls.Add(new LiteralControl(String.Format("<link rel=\"stylesheet\" href=\"{0}\" />", src)));
                }
            }
            else
            {
                // can't add to header, will failback to a simple register script
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, String.Format("<link rel=\"stylesheet\" href=\"{0}\" />", src));
            }
        }

        public static void AddScriptToHeader(string key, string src)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                AddScriptToHeader(key, src, page);
            }
        }

        public static void AddScriptToHeader(string key, string src, Page page)
        {
            CheckParams(key, src);
            src = wrapSrc(src, page);

            if (page.Header != null)
            {
                if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), key))
                {
                    page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, String.Empty);
                    ((HtmlHead)page.Header).Controls.Add(new LiteralControl(String.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", src)));
                }
            }
            else
            {
                // can't add to header, will failback to a simple register script
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), key, String.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", src));
            }
        }

        public static void AddStartupScript(string key, string src)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;
            if (page != null)
            {
                AddStartupScript(key, src, page);
            }
        }


        public static void AddStartupScript(string key, string jsEventCode, Page page)
        {
            jsEventCode = new ECMAScriptPacker(ECMAScriptPacker.PackerEncoding.None, false, false).Pack(jsEventCode);
            CheckParams(key, jsEventCode);

            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), key))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), key, jsEventCode, true);
            }
        }

        private static void CheckParams(string key, string src)
        {
            if (key == null || src == null || key.Trim() == string.Empty || src.Trim() == string.Empty)
            {
                throw new ArgumentNullException();
            }
        }

        public static string wrapSrc(string src, Page page)
        {
            return page.Request.ApplicationPath + src;
        }
    }
}
